package com.kcl.dfss;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kcl.dfss.http.ServerConnection;

public class ChangePwd2Activity extends Activity implements OnClickListener{
	
	private LinearLayout changepwd2_back;
	private TextView text_phone;
	private EditText edit_code;
	private Button btn_code,btn_next2; 
	private String user_phone;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_change_pwd2);
		
		changepwd2_back = (LinearLayout) findViewById(R.id.changepwd2_back);
		text_phone = (TextView) findViewById(R.id.text_phone);
		edit_code = (EditText) findViewById(R.id.edit_code);
		btn_code = (Button) findViewById(R.id.btn_code);
		btn_next2 = (Button) findViewById(R.id.btn_next2);
		
		changepwd2_back.setOnClickListener(this);
		btn_code.setOnClickListener(this);
		btn_next2.setOnClickListener(this);
		btn_next2.setEnabled(false);
		
		Intent intent = getIntent();
		user_phone = intent.getStringExtra("phone");
		text_phone.setText(user_phone);
		
		edit_code.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if(edit_code.getText().length()>0){
					btn_next2.setEnabled(true);
					btn_next2.setBackgroundResource(R.drawable.changepwd_selector);
				}else{
					btn_next2.setEnabled(false);
					btn_next2.setBackgroundResource(R.drawable.changepwd_noclick);
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				
			}
		});
		
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.changepwd2_back:
			finish();
			break;
		case R.id.btn_code:
			TimeCount timeCount = new TimeCount(60000, 1000);
			timeCount.start();
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					ServerConnection.GetCode(text_phone.getText().toString());
				}
			}).start();
			break;
		case R.id.btn_next2:
			Intent intent = new Intent();
			intent.setClass(ChangePwd2Activity.this, ChangePwd3Activity.class);
			intent.putExtra("user_phone", user_phone);
			intent.putExtra("code", edit_code.getText().toString());
//			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			finish();
			break;
		}
	}
	
    class TimeCount extends CountDownTimer{
		
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}
		
		@Override
		public void onTick(long millisUntilFinished) {
			btn_code.setClickable(false);
			btn_code.setText(millisUntilFinished/1000+"秒");
		}
		
		@Override
		public void onFinish() {
			btn_code.setText("重新获取");
			btn_code.setClickable(true);
		}
		
	}
}
